home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap05 / CtlDemo3 / CtlDemo3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.5 KB  |  118 lines

  1. //***********************************************************************
  2. //
  3. //  CtlDemo3.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxext.h>
  9. #include "Resource.h"
  10. #include "CtlDemo3.h"
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_CREATE ()
  30.     ON_WM_DESTROY ()
  31.     ON_COMMAND (IDM_OPTIONS_DISABLE, OnOptionsDisable)
  32.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  33.     ON_UPDATE_COMMAND_UI (IDM_OPTIONS_DISABLE, OnUpdateOptionsDisableUI)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     CString strWndClass = AfxRegisterWndClass (
  39.         0,
  40.         myApp.LoadStandardCursor (IDC_ARROW),
  41.         (HBRUSH) (COLOR_3DFACE + 1),
  42.         myApp.LoadStandardIcon (IDI_APPLICATION)
  43.     );
  44.  
  45.     Create (strWndClass, "CtlDemo3", WS_OVERLAPPEDWINDOW,
  46.         rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
  47. }
  48.  
  49. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  50. {
  51.     if (CFrameWnd::OnCreate (lpcs) == -1)
  52.         return -1;
  53.  
  54.     m_ctlButton1.Create ("Check", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
  55.         CRect (16, 16, 88, 56), this, IDC_BUTTON1);
  56.     m_ctlButton1.LoadBitmaps ("CheckU", "CheckD", "CheckF", "CheckX");
  57.  
  58.     m_hBitmap = ::LoadBitmap (AfxGetInstanceHandle (), "Check");
  59.     m_ctlButton2.Create ("", WS_CHILD | WS_VISIBLE | BS_BITMAP,
  60.         CRect (120, 16, 192, 56), this, IDC_BUTTON2);
  61.     m_ctlButton2.SetBitmap (m_hBitmap);
  62.  
  63.     m_ctlButton3.Create ("", WS_CHILD | WS_VISIBLE | BS_ICON,
  64.         CRect (224, 16, 296, 56), this, IDC_BUTTON3);
  65.     m_ctlButton3.SetIcon (myApp.LoadIcon ("Check"));
  66.  
  67.     m_ctlLabel1.Create ("CBitmapButton", WS_CHILD | WS_VISIBLE |
  68.         SS_CENTER, CRect (8, 72, 96, 100), this);
  69.  
  70.     m_ctlLabel2.Create ("BS_BITMAP", WS_CHILD | WS_VISIBLE | SS_CENTER,
  71.         CRect (112, 72, 200, 100), this);
  72.  
  73.     m_ctlLabel3.Create ("BS_ICON", WS_CHILD | WS_VISIBLE | SS_CENTER,
  74.         CRect (216, 72, 304, 100), this);
  75.  
  76.     CClientDC dc (this);
  77.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  78.  
  79.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  80.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  81.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  82.  
  83.     m_ctlLabel1.SetFont (&m_font, FALSE);
  84.     m_ctlLabel2.SetFont (&m_font, FALSE);
  85.     m_ctlLabel3.SetFont (&m_font, FALSE);
  86.     return 0;
  87. }
  88.  
  89. void CMainWindow::OnDestroy ()
  90. {
  91.     ::DeleteObject (m_hBitmap);
  92. }
  93.  
  94. void CMainWindow::OnOptionsExit ()
  95. {
  96.     SendMessage (WM_CLOSE, 0, 0);
  97. }
  98.  
  99. void CMainWindow::OnOptionsDisable ()
  100. {
  101.     if (m_ctlButton1.IsWindowEnabled ()) {
  102.         m_ctlButton1.EnableWindow (FALSE);
  103.         m_ctlButton2.EnableWindow (FALSE);
  104.         m_ctlButton3.EnableWindow (FALSE);
  105.     }
  106.     else {
  107.         m_ctlButton1.EnableWindow (TRUE);
  108.         m_ctlButton2.EnableWindow (TRUE);
  109.         m_ctlButton3.EnableWindow (TRUE);
  110.     }
  111. }
  112.  
  113. void CMainWindow::OnUpdateOptionsDisableUI (CCmdUI* pCmdUI)
  114. {
  115.     pCmdUI->SetText (m_ctlButton1.IsWindowEnabled () ?
  116.         "&Disable Buttons" : "&Enable Buttons");
  117. }
  118.